using System;
using System.Windows.Forms;

namespace EasyProjects.TimeLogger
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
            tbxUrl.Text = Properties.Settings.Default.EasyProjectsApiUrl;
            tbxLogin.Text = Properties.Settings.Default.User;
            tbxPassword.Text = Properties.Settings.Default.Password;
        }
        
        private void SettingsForm_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
            {
                setPosition();
            }
        }

        /// <summary>
        /// Set form position in reght botton corner
        /// </summary>
        private void setPosition()
        {
            Left = (Screen.PrimaryScreen.WorkingArea.Width - Width);
            Top = (Screen.PrimaryScreen.WorkingArea.Height - Height);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (validateInput())
            {
                bool isNeedUpdate = !tbxUrl.Text.Equals(Properties.Settings.Default.EasyProjectsApiUrl);// user change data need to retrieve new information from server
                Properties.Settings.Default.EasyProjectsApiUrl = tbxUrl.Text;

                if (!isNeedUpdate)
                    isNeedUpdate = !tbxLogin.Text.Equals(Properties.Settings.Default.User);
                Properties.Settings.Default.User = tbxLogin.Text;

                if (!isNeedUpdate)
                    isNeedUpdate = !tbxPassword.Text.Equals(Properties.Settings.Default.Password);
                Properties.Settings.Default.Password = tbxPassword.Text;

                Properties.Settings.Default.Save();
                Close();

                ObjectsHelper.Instance.UpdateClient();

                if (isNeedUpdate)
                    ObjectsHelper.Instance.Update();
            }
        }

        /// <summary>
        /// Validate user input.
        /// </summary>
        /// <returns>True if entered data correct</returns>
        private bool validateInput()
        {
            if (String.IsNullOrEmpty(tbxUrl.Text.Trim()))
            {
                errorProvider1.SetError(tbxUrl, Strings.EmtyUrl);
                return false;
            }

            try
            {
                new Uri(tbxUrl.Text); //Validate url format
            }
            catch (Exception ex)
            {
                errorProvider1.SetError(tbxUrl, ex.Message);
                return false;
            }

            if (String.IsNullOrEmpty(tbxLogin.Text.Trim()))
            {
                errorProvider1.SetError(tbxLogin, Strings.EmptyLogin);
                return false;
            }

            if (String.IsNullOrEmpty(tbxPassword.Text.Trim()))
            {
                errorProvider1.SetError(tbxPassword, Strings.EmptyPassword);
                return false;
            }

            return true;
        }

    }
}